package com.example.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.example.entity.NetfollowsEntity;
import com.example.entity.StudentsEntity;
import com.example.service.NetfollowsService;
import com.example.service.StudentsService;

@RequestMapping("/netfollows")
@Controller
public class NetfollowsController {
	
	@Autowired
	private NetfollowsService netfollowsService;
	@Autowired
	private StudentsService studentService;
	
	
	/**
	 * 查询
	 * @param ne
	 * @return
	 */
	@RequestMapping(path="/find",method=RequestMethod.GET)
	@ResponseBody
	public Map<String, Object> find(NetfollowsEntity ne) {
		Map<String, Object> relMap=new HashMap<String, Object>();
		ne.setOffset((ne.getPage() - 1) * ne.getLimit());
		List<NetfollowsEntity> list=netfollowsService.find(ne);
		int count=netfollowsService.getCount(ne);
		relMap.put("code", "0");
		relMap.put("msg", "success");
		relMap.put("count", count);
		relMap.put("data", list);
		return relMap;
	}
	
	/**
	 * 跟踪
	 * @param ne
	 * @return
	 */
	@RequestMapping(path="/addNetfollows",method=RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> addNetfollows(NetfollowsEntity ne){
		int count=netfollowsService.addNetfollows(ne);
		StudentsEntity se=new StudentsEntity();
		se.setId(ne.getStudentid());
		studentService.payment(se);
		Map<String, Object> relMap=new HashMap<String, Object>();
		relMap.put("code", count>0?"0":"1");
		relMap.put("msg", count>0?"跟踪发送成功":"跟踪发送失败");
		return relMap;
	}
	
	/**
	 * 删除
	 * @param id
	 * @return
	 */
	@RequestMapping(path="/delNetfollows",method=RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> delNetfollows(String id){
		int count=netfollowsService.delNetfollows(Integer.parseInt(id));
		Map<String, Object> relMap=new HashMap<String, Object>();
		relMap.put("code", count>0?"0":"1");
		relMap.put("msg", count>0?"删除成功":"删除失败");
		return relMap;
	}
}
